import { message, Spin } from "antd";
import {
  RouterProvider,
  useLocation,
  createBrowserRouter,
} from "react-router-dom";
import router from "./router";
import { useSelector } from "react-redux";
import { ICommonState } from "./store/index";
import "@/styles/index.less";
import { LoginRes } from "./types/login";
import { useEffect } from "react";
const excludeUrl = ["/login", '/retrievePassword'];
function App() {
  const employee = useSelector<ICommonState>(
    (state) => state.employee
  ) as LoginRes;

  useEffect(() => {
    if (!excludeUrl.includes(router.state.location.pathname) && !employee?.id) {
      if ((router.state.location.pathname === "/resetPassword")) {
        router.navigate("/login");
      } else {
        router.navigate(
          "/login?path=" +
            router.state.location.pathname +
            router.state.location.search
        );
      }
    }
  }, [router.state.location.pathname]);
  const [, contextHolder] = message.useMessage();
  const isLoading = useSelector<ICommonState, boolean>(
    (state) => state.loading
  );
  return (
    <>
      {contextHolder}
      <Spin spinning={isLoading}>
        <RouterProvider router={router} fallbackElement={<div>heh</div>} />
      </Spin>
    </>
  );
}

export default App;
